package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcBerthDAO;
import com.trkj.dao.jpa.CcHospFloorDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcSickOfficeDAO;
import com.trkj.dao.jpa.CcStoreyDAO;
import com.trkj.dao.jpa.CcWardDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.IccJlIccardDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.CcBerth;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcSickOffice;
import com.trkj.pojos.CcStorey;
import com.trkj.pojos.CcWard;
import com.trkj.pojos.JlIccard;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbstaff;
import com.trkj.vo.CcBerthVo;
import com.trkj.vo.CcPatientVo;
import com.trkj.vo.CcStoreyVo;

@Service
@Transactional
public class CcPatientService {
	
	@Autowired
	private CcPatientDAO patientDAO;
	@Autowired
	private IccJlIccardDAO cardDAO;
	@Autowired
	private TbdepartmentDAO tbdepaDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;
	@Autowired
	private CcWardDAO ccWardDAO;
	@Autowired
	private CcHospFloorDAO ccHospDAO;
	@Autowired
	private CcStoreyDAO ccSDAO;
	@Autowired
	private CcBerthDAO ccbDAO;
	@Autowired
	private CcSickOfficeDAO csoDAO;
	public long addCcPatient(CcPatient ccpat){
		ccpat.setPremarks("未入住");
		patientDAO.save(ccpat);
		return ccpat.getPatientid();
	}
	public JlIccard queryOneCard(long cid){
		return cardDAO.findByregisid(cid);
	}
	public CcPatient queryOneCcPatient(BigDecimal cid){
		return patientDAO.findByOneCcPatient(cid);
	}
	public void upCcPatient(long pid,BigDecimal bid){
		CcPatient cc=patientDAO.findOne(pid);
		cc.setPaberthid(bid);
		cc.setPpdate(new Date());
		cc.setPremarks("已入住");
		patientDAO.save(cc);
	}
	public Map<String, Object> queryAllPatient(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcPatient> list=patientDAO.findByAllCcPatient(pr);
		Map<String, Object> map = new HashMap<>();
		map.put("total", patientDAO.count());
		map.put("rows", ccPatientList2VoList(list));
		return map;
	}
	public List<CcPatient> findByAllCcPatient(){
		return patientDAO.findAllCcPatient();
	}
	public CcPatient queryOneCcPatient(long id){
		return patientDAO.findOne(id);
	}
	public List<CcPatient> queryCcPatient(String id){
		return patientDAO.findBypadoctorid(new BigDecimal(id));
	}
	//添加转换科室记录
	public void addCcSickOffice(String offDoctorid,String patientid,String old_offid,
			String pname,String icid,String pofficesid,String padoctorid){
		CcSickOffice cso=new CcSickOffice();
		cso.setOffDoctorid(new BigDecimal(offDoctorid));
		cso.setNewOffid(new BigDecimal(pofficesid));
		cso.setOldOffid(new BigDecimal(old_offid));
		cso.setShiftdate(new Date());
		cso.setOffSickid(new BigDecimal(patientid));
		cso.setShiftreMarks("病情需要");
		csoDAO.save(cso);
		CcPatient cc=new CcPatient();
		cc.setPatientid(Long.parseLong(patientid));
		cc.setPofficesid(new BigDecimal(pofficesid));
		cc.setPadoctorid(new BigDecimal(offDoctorid));
		updateCcPatient(cc);
	}
	
	public void updateCcPatient(CcPatient cc){
		CcPatient cp=patientDAO.findOne(cc.getPatientid());
		cp.setPadoctorid(cc.getPadoctorid());
		cp.setPofficesid(cc.getPofficesid());
		patientDAO.save(cp);
	}
	public Map<String, Object> querynullpaberthid(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcPatient> list=patientDAO.findBypaberthidnull(pr);
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", patientDAO.findBypaberthidnullCount());
		map.put("rows", ccPatientList2VoList(list));
		return map;
	}
	public Map<String, Object> querynotnullpaberthid(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcPatient> list=patientDAO.findByPaberthidnotnull(pr);
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", patientDAO.findBypaberthidnotnullCount());
		map.put("rows", ccPatientList2VoList(list));
		return map;
	}
	private List<CcPatientVo> ccPatientList2VoList(List<CcPatient> list){
		if(list == null) return null;
		List<CcPatientVo> listVo = new ArrayList<>();
		for(CcPatient cc:list){
			CcPatientVo ccVo=new CcPatientVo();
			if(cc.getPadoctorid()!=null){
				ccVo.setStaffNo(cc.getPadoctorid().longValue());
				Tbstaff sta= staffDAO.findOne(ccVo.getStaffNo());
				ccVo.setStaffName(sta.getStaffName());
			}
			ccVo.setDepartmentNo(cc.getPofficesid().longValue());
			Tbdepartment tb=tbdepaDAO.findOne(ccVo.getDepartmentNo());
			ccVo.setDepartmentName(tb.getDepartmentName());
			ccVo.setAddress(cc.getAddress());
			ccVo.setIcid(cc.getIcid());
			ccVo.setPaage(cc.getPaage());
			ccVo.setPaberthid(cc.getPaberthid());
			ccVo.setPcard(cc.getPcard());
			ccVo.setPofficesid(cc.getPofficesid());
			ccVo.setPatientid(cc.getPatientid());
			ccVo.setPname(cc.getPname());
			ccVo.setPpdate(cc.getPpdate());
			if(cc.getPremarks()==null){
				ccVo.setPremarks("未入住");
			}else{
				ccVo.setPremarks(cc.getPremarks());
			}
			ccVo.setPsex(cc.getPsex());
			ccVo.setPweight(cc.getPweight());
			if(cc.getPaberthid()!=null){
				ccVo.setBerthid(cc.getPaberthid().longValue());
				CcBerth cb=ccbDAO.findOne(ccVo.getBerthid());
				ccVo.setBname(cb.getBname());
				CcWard cw=ccWardDAO.findOne(cb.getBwardno().longValue());
				ccVo.setWmoney(cw.getWmoney().toString());
				ccVo.setWname(cw.getWname());
				CcStorey cst = ccSDAO.findOne(cw.getWstoId().longValue());
				ccVo.setWaddress(ccHospDAO.queryName(cst.getSfloorId().longValue()) + ccSDAO.queryName(cw.getWstoId().longValue()));
				
			}
			
			listVo.add(ccVo);
		}
		return listVo;
	}
}
